import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";

@IonicPage({
    segment: 'member/transpond',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-transpond',
    templateUrl: 'person-transpond.html',
})
export class PersonTranspondPage {
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public storage: StorageProvider,
        public validate: ValidateProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.getData(null);
    }

    page = 1;
    lists = [];

    getData(infiniteScroll) {
        let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
        let token = tokenInfo['token'];

        let apiUrl = "/user_info/my_share/token/" + token + "/page/" + this.page;
        this.httpService.requestData(apiUrl, (response) => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['lists'].length < data['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    /**
     * 跳转到信息详情
     * */
    goInformationDetail(amoy_id){
        this.navCtrl.push('InformationDetailPage', {id: amoy_id});
    }
}
